package com.yangcangclub.commons.utils;


import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * Created by Administrator on 2016/4/11.
 */
public class DateUtil {

    /**
     * long类型转换为 yyyy-MM-dd HH:mm
     * @param t
     * @return
     */
    public static String LongToMinuteString(long t){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
        return sdf.format(new Date(t));
    }

    /**
     * Date yyyy-MM-dd HH:mm
     * @param t
     * @return
     */
    public static String DateToMinuteString(Date t){
        if(t==null) return "";
        return LongToMinuteString(t.getTime());
    }

    /**
     * 获取几天前的日期，yyyy-MM-dd HH:mm:ss
     * @param 
     * @return
     */
    public static String getSomeDaysBeforeDateString(int n){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, - n);
        Date need = c.getTime();
        return sdf.format(need);
    }

    /**
     * 获取今天的零时，如“2016-04-19 00:00:00”
     * @return
     */
    public static String getTodayZeroDatetimeString(){
        return getSomeDaysBeforeDateNoHoursString(0)+" 00:00:00";
    }

    /**
     * 获取昨天的零时，如“2016-04-19 00:00:00”
     * @return
     */
    public static String getYesterdayZeroDatetimeString(){
        return getSomeDaysBeforeDateNoHoursString(1)+" 00:00:00";
    }

    /**
     * 获取几天前的日期，yyyy-MM-dd
     * @param
     * @return
     */
    public static String getSomeDaysBeforeDateNoHoursString(int n){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, - n);
        Date need = c.getTime();
        return sdf.format(need);
    }

    /**
     * 将短时间格式时间转换为字符串 yyyy-MM-dd
     *
     * @param dateDate
     * @return
     */
    public static String dateToStr(java.util.Date dateDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(dateDate);
        return dateString;
    }

    /**
     * 将字符串时间转换为短时间格式 yyyy-MM-dd
     * @param strDate
     * @return
     */
    public static Date strToDate(String strDate) throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date dateString = formatter.parse(strDate);
        return dateString;
    }

    /**
     * 将短时间格式时间转换为字符串 yyyy-MM-dd
     *
     * @param dateDate
     * @return
     */
    public static String dateToStrWithTime(java.util.Date dateDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(dateDate);
        return dateString;
    }

    public static long getDateTime(String lastDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date = formatter.parse(lastDate);
            long dateTime = date.getTime();
            return dateTime;
        } catch (ParseException e) {
        }
        return 0l;
    }

    /**
     * 根据几天前后日期
     * @param i
     * @return
     * @throws java.text.ParseException
     */
    public static String getDateBeforeToday(int i)  {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

            Calendar c = Calendar.getInstance();
            c.add(Calendar.DATE, - i);
            Date monday = c.getTime();
            String preMonday = sdf.format(monday);
            return preMonday;
        } catch (Throwable t) {
            //ignore
            return DateUtil.dateToStr(new Date());
        }
    }

    public static String getDayBeforeToday(int i) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

            Calendar c = Calendar.getInstance();
            c.add(Calendar.DATE, - i);
            Date monday = c.getTime();
            String preMonday = sdf.format(monday);
            int datenum=c.get(Calendar.DATE);
            return datenum+"";
        } catch (Throwable t) {
            //ignore
            return "0";
        }

    }


    /**
     * 日期转换成字符串
     * @param source
     * @return
     */
    public static String DateConvertToString(Date source) {
        if(null == source) {
            return null;
        }
        String target = null;
        try {
            target =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(source);
        } catch (Throwable t) {
            //容错
        }
        return target;
    }

    /**
     * 日期转换成字符串到天
     * @param source
     * @return
     */
    public static String DateConvertToStringDay(Date source) {
        if(null == source) {
            return null;
        }
        String target = null;
        try {
            target =  new SimpleDateFormat("yyyy-MM-dd").format(source);
        } catch (Throwable t) {
            //容错
        }
        return target;
    }

    public static String DateConvertToStringMonth(Date source) {
        if(null == source) {
            return null;
        }
        String target = null;
        try {
            target =  new SimpleDateFormat("yyyy-MM").format(source);
        } catch (Throwable t) {
            //容错
        }
        return target;
    }

    /**
     * 日期转换成字符串到小时点
     * @param source
     * @return
     */
    public static String DateConvertToStringHour(Date source) {
        if(null == source) {
            return null;
        }
        String target = null;
        try {
            target =  new SimpleDateFormat("HH:mm:ss").format(source);
        } catch (Throwable t) {
            //容错
        }
        return target;
    }

    /**
     * 字符串转换成日期
     * @param source
     * @return
     */
    public static Date StringConvertToDate(String source) {
        if(null == source) {
            return null;
        }
        Date target = null;
        try {
            target =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(source);
        } catch (Throwable t) {
            //容错
        }
        return target;
    }

    public static Date StringConvertToDateYMD(String source) {
        if(null == source) {
            return null;
        }
        Date target = null;
        try {
            target =  new SimpleDateFormat("yyyy-MM-dd").parse(source);
        } catch (Throwable t) {
            //容错
        }
        return target;
    }

    /**
     * 比较两个日期相差天数 date_1-date_2 大于 1，等于 0 小于 -1
     * @param date_1
     * @param date_2
     * @return
     */
    public static int compare_date(String date_1, String date_2) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date dt1 = df.parse(date_1);
            Date dt2 = df.parse(date_2);
            if (dt1.getTime() > dt2.getTime()) {
                return 1;
            } else if (dt1.getTime() < dt2.getTime()) {
                return -1;
            } else {
                return 0;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return 0;
    }

    /**
     * 计算两个日期间相差的月数
     * @param date1 <String>
     * @param date2 <String>
     * @return int
     * @throws ParseException
     */
    public static int getMonthSpace(String date1, String date2)
            throws ParseException {

        int result = 0;

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();

        c1.setTime(sdf.parse(date1));
        c2.setTime(sdf.parse(date2));

        result =(c2.get(Calendar.YEAR)- c1.get(Calendar.YEAR))*12
                +c2.get(Calendar.MONTH) - c1.get(Calendar.MONTH);
        return result == 0 ? 1 : Math.abs(result);

    }

    /**
     * 将日期往后延迟几个月后 再和当前时间比较
     * @param date2
     * @param latencyMonth
     * @return
     */
    public static int getMonthSpaceFromCurrentDate(String date2,int latencyMonth) {

        int result = 0;

        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

            Calendar c1 = Calendar.getInstance();
            Calendar c2 = Calendar.getInstance();

            c1.setTime(new Date());
            c2.setTime(sdf.parse(date2));
            int month = c2.get(Calendar.MONTH);
            c2.set(Calendar.MONTH,month+latencyMonth);

            result =(c2.get(Calendar.YEAR)- c1.get(Calendar.YEAR))*12
                    +c2.get(Calendar.MONTH) - c1.get(Calendar.MONTH);
            return result == 0 ? 1 : Math.abs(result);
        } catch (Throwable t) {
            //ig
        }

       return 0;
    }


    /**
     * 获取现在时间
     *
     * @return返回字符串格式 yyyy-MM-dd HH:mm:ss
     */
    public static String getNowDate() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    /**
     * 获取现在时间
     *
     * @return返回字符串格式 yyyyMMddHHmm
     */
    public static String getNowDateToMinute() {
        Date currentTime = new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmm");
        String dateString = sdf.format(currentTime);
        return dateString;
    }

    /**
     * 获取现在时间
     *
     * @return返回字符串格式 yyyy-MM-dd
     */
    public static String getNowDateToDay() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    /**
     * 获得今天日期：格式为：yyyyMMdd
     * @return
     */
    public static String getNowyyyyMMdd() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        String dateString = formatter.format(currentTime);
        return dateString;
    }



    /**
     * 获得指定日期的前n天
     *
     * @param specifiedDay
     * @param n
     * @return
     * @throws Exception
     */
    public static String getSpecifiedDayBefore(String specifiedDay,int n) {//可以用new Date().toLocalString()传递参数
        Calendar c = Calendar.getInstance();
        Date date = null;
        try {
            date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        c.setTime(date);
        int day = c.get(Calendar.DATE);
        c.set(Calendar.DATE, day - n);

        String dayBefore = new SimpleDateFormat("yyyy-MM-dd").format(c
                .getTime());
        return dayBefore;
    }

    /**
     * 获得指定日期的后n天
     *
     * @param specifiedDay
     * @param n
     * @return
     */
    public static String getSpecifiedDayAfter(String specifiedDay,int n) {
        Calendar c = Calendar.getInstance();
        Date date = null;
        try {
            date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        c.setTime(date);
        int day = c.get(Calendar.DATE);
        c.set(Calendar.DATE, day + n);

        String dayAfter = new SimpleDateFormat("yyyy-MM-dd")
                .format(c.getTime());
        return dayAfter;
    }

    /**
     * 获得几n个月后的年月
     *
     * @param yyyyMMdd
     * @param n
     * @return
     */
    public static String getSpecifiedMonthAfter(String yyyyMMdd,int n) {
        Calendar c = Calendar.getInstance();
        Date date = null;
        try {
            date = new SimpleDateFormat("yyyy-MM-dd").parse(yyyyMMdd);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        c.setTime(date);
        int month = c.get(Calendar.MONTH);
        c.set(Calendar.MONTH, month + n);

        String dayAfter = new SimpleDateFormat("yyyy-MM")
                .format(c.getTime());
        return dayAfter;
    }

    /**
     * 获得几n个月后的年月日
     *
     * @param yyyyMMdd
     * @param n
     * @return
     */
    public static String getSpecifiedMonthAfterYMd(String yyyyMMdd,int n) {
        Calendar c = Calendar.getInstance();
        Date date = null;
        try {
            date = new SimpleDateFormat("yyyy-MM-dd").parse(yyyyMMdd);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        c.setTime(date);
        int month = c.get(Calendar.MONTH);
        c.set(Calendar.MONTH, month + n);

        String dayAfter = new SimpleDateFormat("yyyyMMdd")
                .format(c.getTime());
        return dayAfter;
    }


    /**
     * 获取几天后的时间和日期：yyyy-MM-dd HH:mm:dd
     * @param n
     * @return
     */
    public  static String getDateTimeAfterToday(int n){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, + n);
        Date need = c.getTime();
        return sdf.format(need);
    }

    public static Date longConvertDate(long DateTimeStamp) {
        try {
            SimpleDateFormat format =  new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
            String d = format.format(DateTimeStamp);
            Date date=format.parse(d);

            return date;
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return null;
    }


    /**
     * 将短时间格式时间转换为字符串 yyyy-MM-dd
     *
     * @param dateDate
     * @return
     */
    public static String dateToStrWithTimeAmPm(java.util.Date dateDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm");
        String dateString = formatter.format(dateDate);

        GregorianCalendar ca = new GregorianCalendar();
        ca.setGregorianChange(dateDate);
        int i = ca.get(GregorianCalendar.AM_PM);

        if(1==i) {
            dateString=dateString+" AM";
        } else if(0==i) {
            dateString=dateString+" PM";
        }

        return dateString;
    }

    /**
     * 判断是不是yyyyMMdd结构.2000-01-01之前的直接返回FALSE
     * @param date
     * @return
     */
    public static boolean isYYYYMMDD(int date){
        if (date<20000101) return false;//2000-01-01之前的直接返回FALSE
        int year = date/10000;
        int month = date%10000/100;
        int day=date%100;
        if(2000<year&&year<10000
                &&0<month&&month<13
                &&0<day&&day<32){
            return true;
        }
        return false;
    }

    /**
     * 判断是不是yyyyMMW结构.2016011之前的直接返回FALSE
     * @param date 2010051 最后一位只能为1,2,3,4
     * @return
     */
    public static boolean isYYYYMMW(int date){
        if (date<2000011) return false;//2000-01-1之前的直接返回FALSE
        int year = date/1000;
        int month = date%1000/10;
        int day=date%10;
        if(2000<year&&year<10000
                &&-1<month&&month<13
                &&-1<day&&day<5){
            return true;
        }
        return false;
    }

    
    public static boolean isYYYYMMDD(String date){
        date=date.replace("-","");
        int dd=Integer.parseInt(date);
        return isYYYYMMDD(dd);
    }

    public static boolean isOneMonthSpaceFromYYYYMM(String date1, String date2){
        try {
            int result = 0;
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
    
            Calendar c1 = Calendar.getInstance();
            Calendar c2 = Calendar.getInstance();
    
                    
            c1.setTime(sdf.parse(date1));
            c2.setTime(sdf.parse(date2));
    
            result =(c2.get(Calendar.YEAR)- c1.get(Calendar.YEAR))*12
                    +c2.get(Calendar.MONTH) - c1.get(Calendar.MONTH);
            
            return result <2&&result>-1 ;
        } catch (ParseException e) {
            e.printStackTrace();
            return false;
        }

    }



    public static void main(String[] args) throws ParseException {
        String nowDate = DateUtil.getNowDateToDay();

        int monthNum  = DateUtil.getMonthSpace("2016-08-31",nowDate);

        System.out.println(monthNum);
    }


}
